// pages/ebook/description/catalog/book/book.js
var filePath;
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    filePath: '',
    tempFilePaths: [],
    initFontSize: '14',
    colorArr: [{
      value: '#f7eee5',
      name: '米白',
      font: ''
    }, {
      value: '#e9dfc7',
      name: '纸张',
      font: '',
      id: "font_normal"
    }, {
      value: '#a4a4a4',
      name: '浅灰',
      font: ''
    }, {
      value: '#cdefce',
      name: '护眼',
      font: ''
    }, {
      value: '#283548',
      name: '灰蓝',
      font: '#7685a2',
      bottomcolor: '#fff'
    }, {
      value: '#0f1410',
      name: '夜间',
      font: '#4e534f',
      bottomcolor: 'rgba(255,255,255,0.7)',
      id: "font_night"
    }],
    nav: 'none',
    ziti: 'none',
    _num: 1,
    bodyColor: '#fff',
    zj: 'none'
  },

  /**
 
 * 下载文件并预览
 
 */

  downloadFile: function (e) {
    var that = this;
    let type = e.currentTarget.dataset.type;
    let url = e.currentTarget.dataset.url;
    switch (type) {

      case "pdf":

        url += 'pdf';

        break;

      case "word":

        url += 'docx';

        break;

      case "excel":

        url += 'xlsx';

        break;

      default:

        url += 'pptx';

        break;

    }

    wx.downloadFile({

      url: url,

      header: {},
      success: function (res) {
        var filePath = res.tempFilePath;
        console.log(filePath);
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        })
        wx.saveFile({
          tempFilePath: filePath,
          success: function (res) {
            var savedFilePath = res.savedFilePath;
            that.data.filePath = savedFilePath;
            console.log(that.data.filePath);
          }
        })
        // that.data.filePath = filePath;
      },

      fail: function (res) {
        console.log('文件下载失败');
      },
      complete: function (res) { },
    }),
      console.log(that.data.filePath);

    wx.getSavedFileList({
      success: function (res) {
        console.log(res.fileList)
      }
    })

    /*wx.getSavedFileList({
      success: function (res) {
        if (res.fileList.length > 0) {
          wx.removeSavedFile({
            filePath: res.fileList[0].filePath,
            complete: function (res) {
              console.log(res)
            }
          })
        }
      }
    })*/

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 本地提取字号大小
    var that = this;
    wx.getStorage({
      key: 'initFontSize',
      success: function (res) {
        // console.log(res.data)
        that.setData({
          initFontSize: res.data
        })
      }
    })
    //存储背景色
    wx.getStorage({
      key: 'bodyColor',
      success: function (res) {
        console.log(res.data)
        that.setData({
          bodyColor: res.data
        })
      }
    })
    wx.getStorage({
      key: '_num',
      success: function (res) {
        // console.log(res.data)
        that.setData({
          _num: res.data
        })
      }
    })
  },
  /**
   * 以下部分为界面响应
   */
  //点击设置出现窗口
  show: function (e) {
    var name = e.currentTarget.dataset.type;
    if (name == "设置") {
      if (this.data.ziti == 'none') {
        this.setData({
          ziti: 'block'
        })
      } else {
        this.setData({
          ziti: 'none'
        })
      }
    }
  },
  //字体变大
  bindBig: function () {
    var that = this;
    if (that.data.initFontSize > 20) {
      return;
    }
    var FontSize = parseInt(that.data.initFontSize)
    that.setData({
      initFontSize: FontSize += 1
    })
    // console.log(that.data.initFontSize)
    wx.setStorage({
      key: "initFontSize",
      data: that.data.initFontSize
    })
  },
  //字体变小
  bindSmall: function () {
    var that = this;
    if (that.data.initFontSize < 12) {
      return;
    }
    var FontSize = parseInt(that.data.initFontSize)
    that.setData({
      initFontSize: FontSize -= 1
    })
    // console.log(that.data.initFontSize)
    wx.setStorage({
      key: "initFontSize",
      data: that.data.initFontSize
    })
  },
  //点击中间区域显示底部导航
  midaction: function () {
    if (this.data.nav == 'none') {
      this.setData({
        nav: 'block'
      })
    } else {
      this.setData({
        nav: 'none',
        ziti: 'none'
      })

    }
  },

  //选择背景色
  bgChange: function (e) {
    var that = this;
    console.log(e.target.dataset.num)
    // console.log(e)
    this.setData({
      _num: e.target.dataset.num,
      bodyColor: this.data.colorArr[e.target.dataset.num].value
    })
    wx.setStorage({
      key: "bodyColor",
      data: this.data.colorArr[e.target.dataset.num].value,
    })
    wx.setStorage({
      key: "_num",
      data: e.target.dataset.num
    })
    wx.getStorage({
      key: 'bodyColor',
      success: function (res) {
        console.log(res.data)
        that.setData({
          bodyColor: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.editTabBar1();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})